
class getEdgePoints {
	constructor(){
        this.allBaseLines = [];
        this.pts=[];
       // this.json='[{"lat":37.94,"lng":-121.94,"username":"user_1","address":"address_1","phone":"15219595966","time":20,"lbs":247},{"lat":37.9,"lng":-122.3,"username":"user_2","address":"address_2","phone":"15219595966","time":20,"lbs":247},{"lat":37.88,"lng":-122.3,"username":"user_3","address":"address_3","phone":"15219595966","time":20,"lbs":247},{"lat":37.78,"lng":-122.27,"username":"user_4","address":"address_4","phone":"15219595966","time":20,"lbs":247}]'
        this.points=[];

    }
    static getInstance() {
        if(!getEdgePoints.instance){
            getEdgePoints.instance = new getEdgePoints();
        }
        return getEdgePoints.instance;
    }





 qhPlotConvexHull(data)			//Draw convex hull in graph
{

	this.pts = data;
    this.points=[];
    this.allBaseLines = [];

    for(let i=0;i<this.pts.length;i++ ) {
    	this.pts[i].lng = Math.abs(this.pts[i].lng)
	}

	this.ch = this.getConvexHull(this.pts);	//Get Quick Hull Points
	this.plotIntermediateBL();
	return this.getPoints();
}

getPoints(){

		let ret = [];
    console.log(this.points,'this.points')
		let frist = this.points.shift();
		ret.push(frist[0]);
	    ret.push(frist[1]);
	    this.points.forEach(function (items) {
			 ret.push( items[1])
        })
	   /* for(let i=0; i <this.points.length-1;i++ ) {
	    	console.log(1)
	    	ret.push( this.points[i][0]);
            ret.push( this.points[i][1]);
		}*/

	    return ret;
	    return orderEdgePoints.getInstance().orderPoints(ret);

}


 plotIntermediateBL()
    {

        var l = this.allBaseLines.shift();		//Pop first item
        if (l)	//if item exists and popped
        {
            this.plotIntermediateBL();
        }
        else
        {
            for (var idx in this.ch)
            {
                var baseLine = this.ch[idx];
                this.points.push(baseLine)
            }
          


        }
    }
 getConvexHull(points)		//Quick hull Algorithm Simulation (Recursive) - Find Points
{
	//find first baseline
	var maxX, minX;			//All points are greater than 0, so no initialization is needed
	var maxPt, minPt;
	for (var idx in points)		//Find 2 extreme points to devide them
	{
		var pt = points[idx];
		if (pt['lng'] > maxX || !maxX)
		{

			maxPt = pt;
			maxX = pt['lng'];
		}
		if (pt['lng'] < minX || !minX)
		{
			minPt = pt;
			minX = pt['lng'];

		}
	}
	//console.log(minPt,maxPt,'max')
	var ch = [].concat(this.buildConvexHull([minPt, maxPt], points),
				this.buildConvexHull([maxPt, minPt], points));	//Create upper and lower points and concate them and store
	return ch;
}

//quickhull for one part
 buildConvexHull(baseLine, points)
{
	this.allBaseLines.push(baseLine);		//Over rites in global value, but not making any problem, because the data is stored then in function
	var convexHullBaseLines = [];
	var t = this.findMostDistantPointFromBaseLine(baseLine, points);
	if (t.maxPoint.length)
	{ // if there is still a point "outside" the base line
		convexHullBaseLines =
			convexHullBaseLines.concat(
				this.buildConvexHull( [baseLine[0],t.maxPoint], t.newPoints)
			);
		convexHullBaseLines =
			convexHullBaseLines.concat(
				this.buildConvexHull( [t.maxPoint,baseLine[1]], t.newPoints)
			);
		return convexHullBaseLines;
	}
	else
	{	// if there is no more point "outside" the base line, the current base line is part of the convex hull
		return [baseLine];
	}
}

 findMostDistantPointFromBaseLine(baseLine, points)		//Find the most distant point
{
	var maxD = 0;
	var maxPt = new Array();
	var newPoints = new Array();
	for (var idx in points)
	{
		var pt = points[idx];
		var d = this.getDistant(pt, baseLine);
		if ( d > 0)
		{
			newPoints.push(pt);
		}
		else
		{
			continue;
		}
		if ( d > maxD )
		{
			maxD = d;
			maxPt = pt;
		}
	}
	return {'maxPoint':maxPt, 'newPoints':newPoints};
}

 getDistant(cpt, bl)	//Find distence of point and line
{
	//return ( (cpt[0]*bl[0][1]+bl[0][0]*bl[1][0]+bl[1][1]*cpt[1]-cpt[1]*bl[0][0]-bl[0][1]*bl[1][0]-bl[1][1]*cpt[0])/Math.sqrt( (bl[0][0]-bl[0][1])*(bl[0][0]-bl[0][1]) + (bl[1][0]-bl[1][1])*(bl[1][0]-bl[1][1]) ) );

   
	var Vy = bl[1]['lng'] - bl[0]['lng'];
	var Vx = bl[0]['lat'] - bl[1]['lat'];
	return (Vx * (cpt['lng'] - bl[0]['lng']) + Vy * (cpt['lat'] -bl[0]['lat']));

}




}





